package com.shenjian.settings.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shenjian.commons.exception.CustomException;
import com.shenjian.settings.dao.DicTypeDao;
import com.shenjian.settings.entity.DicType;
import com.shenjian.settings.service.DicTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (DicType)表服务实现类
 *
 * @author makejava
 * @since 2022-06-29 15:30:47
 */
@Service("dicTypeService")
public class DicTypeServiceImpl implements DicTypeService {
    @Autowired
    private DicTypeDao dicTypeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param code 主键
     * @return 实例对象
     */
    @Override
    public DicType findById(String code) {
        return this.dicTypeDao.selectById(code);
    }

    @Override
    public List<DicType> list() {
        return dicTypeDao.select(null);
    }

    /**
     * 分页查询
     *
     * @param pageNumber 当前页码
     * @param pageSize   每页条数
     * @param dicType    筛选条件
     * @return 查询结果
     */
    @Override
    public Map<String, Object> listPage(Integer pageNumber, Integer pageSize, DicType dicType) {
        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<DicType> pageInfo = new PageInfo<>(dicTypeDao.select(dicType));
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("list", pageInfo.getList());
        pageMap.put("total", pageInfo.getTotal());
        return pageMap;
    }

    /**
     * 新增数据
     *
     * @param dicType 实例对象
     * @return 实例对象
     */
    @Override
    public boolean save(DicType dicType) {
        //判断用户名是否重复
        DicType dbDicType = dicTypeDao.selectById(dicType.getCode());
        //找到数据苦的值
        if (dbDicType != null) {
            throw new CustomException(-10, "数据字典类型编码已存在");
        }
        return dicTypeDao.insert(dicType)>0;
    }

    /**
     *
     * @param dicType 实例对象
     * @return
     */
    @Override
    public boolean edit(DicType dicType) {
        return dicTypeDao.update(dicType)>0;
    }

    /**
     * 通过主键删除数据
     *
     * @param code 主键
     * @return 是否成功
     */
    @Override
    public boolean remove(String code) {
        return dicTypeDao.deleteById(code) > 0;
    }

    /**
     * 通过主键数组删除数据
     * @param codes 主键数组
     * @return
     */
    @Override
    public boolean removeBatch(String[] codes) {
        return dicTypeDao.deleteBatch(codes) > 0;
    }
}
